package com.zdft.promotion.common.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;

@Configuration
public class RestfulClient {

    @Value("${url.mdm}")
    String mdmUrl;

    @Autowired
    RestTemplate restTemplate;

    static String url;

    static RestTemplate client;

    @PostConstruct
    public void init(){
        url = mdmUrl;
        client = restTemplate;
    }

    public static String getRestResult(String paramJson, String method) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");// 请求头
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity requestEntity = new HttpEntity(paramJson, headers);
        ResponseEntity<String> result = client
                .postForEntity(url + method, requestEntity, String.class);
        if (result.getStatusCode().equals(HttpStatus.OK)) {
            return result.getBody();
        }
        return null;
    }

}
